package com.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.entity.Happy;
import com.ssh.server.HappyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class HappyAction extends ActionSupport{
    private Map<String,Object> jsonMap;

    public Map<String, Object> getJsonMap() {
        return jsonMap;
    }

    public void setJsonMap(Map<String, Object> jsonMap) {
        this.jsonMap = jsonMap;
    }
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Autowired
    private HappyServer happyServer = null;

    public String showAllHappy() throws Exception{
        List<Happy> happyList = happyServer.getAllHappy();
        //把结果传递给前端jsp页面
        ActionContext.getContext().getValueStack().set("happyList",happyList);
        return this.SUCCESS;
    }
    public String isWin(){
       System.out.println("前端传过来的id"+id);
       boolean flag = happyServer.isWin(id);
       System.out.println("结果"+flag);
       jsonMap = new HashMap<String,Object>();
       jsonMap.put("status",1);//代表没有异常  成功生成数据
        if (flag==true){
            jsonMap.put("message","恭喜!已经获奖了（给前端工程师看）");
            jsonMap.put("data",true);
        }else{
            jsonMap.put("message","不好意思!你输了（给前端工程师看）");
            jsonMap.put("data",false);
        }
        return "jsonok";
    }
}
